package com.ean.backpack.other;

import java.util.regex.Pattern;

public class DestinationFormatter {

	private String destination;
	private String country;
	private String city;
	private String state;

	public String getDestination() {
		return destination;
	}

	public void setDestination(String destination) {
		this.destination = destination;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public DestinationFormatter() {
		this.city = "";
		this.state = "";
		this.country = "";
	}

	public DestinationFormatter(String destination) {
		this.destination = destination;
		this.city = "";
		this.state = "";
		this.country = "";
	}

	public boolean isValidDestination() {

		return isValidDestination(this.destination);
	}

	public boolean isValidDestination(String destination) {
		Pattern destinationPattern = Pattern.compile(
				"^(([0-9]*)([a-zA-Z ]+[.'-]?)+)(,(([a-zA-Z ]+))?){0,2}(\\([a-zA-Z ]+\\))?$",
				Pattern.UNICODE_CASE);

		return destinationPattern.matcher(destination).find();
	}

	public void process(String destination, String regex){
		if(this.isValidDestination(destination)){
			String[] split = destination.split(regex);
			this.destination= destination;
			switch (split.length) {
			case 0: 
				this.city = split[0].trim();
				this.state = "";
				this.country = "";
				break;
			case 1:
				this.city = split[0].trim();
				this.state = "";
				this.country = "";
				break;
			case 2:
				this.city = split[0].trim();
				this.country = split[1].trim();
				this.state = "";
				break;
			case 3:
				this.city = split[0].trim();
				this.state = split[1].trim();
				this.country = split[2].trim();
				break;
			default:
				this.city = "";
				this.state = "";
				this.country = "";
				break;
			}
		} else {
			
		}
	}

	@Override
	public String toString() {
		return String
				.format("DestinationFormatter \n\t[destination=%s, \n\tcountry=%s, \n\tcity=%s, \n\tstate=%s]",
						destination, country, city, state);
	}


}
